﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using DACU.Properties;

namespace DACU.Player
{
	[Serializable, DebuggerDisplay("Count = {Count}"), DebuggerTypeProxy(typeof(CollectionDebugView<>))]
	public class History
	{
		List<int> history = new List<int>(300);

		public History()
		{
		}

		public History(IEnumerable<int> collection)
		{
			history.AddRange(collection);
		}

		public int this[int index]
		{
			get { return history[index]; }
		}

		//public bool HasPrevious
		//{
		//    get { return history.Count > 1; }
		//}

		public int Last
		{
			get
			{
				if (IsEmpty)
					throw new InvalidOperationException(Resources.InvalidOperationException_EmptyHistory);
				return history[history.Count - 1];
			}
		}

		public int Count
		{
			get { return history.Count; }
		}

		public bool IsEmpty
		{
			get { return history.Count < 1; }
		}

		private bool IsReadOnly { get; set; }

		public void InHistory(int trackId)
		{
			if(IsReadOnly) throw new InvalidOperationException("Resources.InvalidOperationException_HistoryReadOnly");
			if ((history.Count + 1) > 300)
				history.RemoveAt(0);
			history.Add(trackId);
		}

		public int OutHistory()
		{
			if (IsReadOnly) throw new InvalidOperationException("Resources.InvalidOperationException_HistoryReadOnly");
			if (history.Count > 0)
				history.RemoveAt(history.Count - 1);
			return history.Count > 0 ? Last : -1;
		}

		public void Clear()
		{
			if (IsReadOnly) throw new InvalidOperationException("Resources.InvalidOperationException_HistoryReadOnly");
			history.Clear();
		}

		public void Remove(int value)
		{
			if(!IsReadOnly)
			{
				int index = history.BinarySearch(value);
				if(index>-1)
					history.RemoveAt(index);
			}
		}

		public History AsReadOnly()
		{
			return new History(history) {IsReadOnly = true};
		}
	}
}
